//
//  ECMainTabBar.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/10/22.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECMainTabBar.h"

#define CentenrBTW 50 * PX_RELATIVE375

@interface ECMainTabBar()
@property (nonatomic,strong) UIView * centerBTBackView;
@end

@implementation ECMainTabBar
@synthesize centerBT = _centerBT;
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.centerBTBackView];
        [self addSubview:self.centerBT];
        [self setTintColor:ECCOLOR(0x41d95a)];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = self.width;
    
    CGFloat btnx = 0;
    CGFloat btny = 0;
    //5.0是tabbar中的控件的数量
    CGFloat width = self.bounds.size.width/(self.items.count + 1);
    CGFloat height = 48 * PX_RELATIVE375;
    
    int i=0;
    for (UIView *btn in self.subviews) {
        //判断是否是系统自带的UITabBarButton类型的控件
        if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (i<2) {
                btnx = i*width;
            }else{
                btnx = (i+1)*width;
            }
            self.centerBTBackView.frame = CGRectMake(2 * width, 0, width, height);
            btn.frame = CGRectMake(btnx, btny, width, height);
            
            i++;
        }
        
    }
    
    //设置自定义button的位置
    self.centerBT.center = CGPointMake(w*0.5, CentenrBTW / 2 - 12 * PX_RELATIVE375);
    [self bringSubviewToFront:self.centerBT];
}

- (UIButton *)centerBT {
    if (!_centerBT) {
        _centerBT = [UIButton buttonWithType:UIButtonTypeCustom];
        [_centerBT setBackgroundImage:[[UIImage imageNamed:@"tab_btn_dingdanxinzeng_disable"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [_centerBT setTitle:@"" forState:UIControlStateNormal];
        _centerBT.bounds = CGRectMake(0, 0, CentenrBTW, CentenrBTW);
        _centerBT.clipsToBounds = YES;
        _centerBT.layer.cornerRadius = CentenrBTW / 2;
    }
    return _centerBT;
}

- (UIView *)centerBTBackView {
    if (!_centerBTBackView) {
        _centerBTBackView = [UIView new];
    }
    return _centerBTBackView;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.hidden) {
        if (CGRectContainsPoint(self.centerBT.bounds, [self convertPoint:point toView:self.centerBT]) || CGRectContainsPoint(self.centerBTBackView.bounds, [self convertPoint:point toView:self.centerBTBackView])) {
            return self.centerBT;
        }
    }
    return [super hitTest:point withEvent:event];
}

@end
